package cn.itcast.controller;

import cn.itcast.service.UserService;
import cn.itcast.DTO.PageQueryDTO;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //新增用户
    @PostMapping("/add")
    public Result<User> add(@RequestBody User user){
        User add = userService.add(user);
        return Result.success(add);
    }

    //根据id查询用户
    @GetMapping("/{id}")
    public Result<User> findById(@PathVariable Integer id) {
        User user = userService.findById(id);
        return Result.success(user);
    }

    //分页查询用户
    @GetMapping("/page")
    public Result<PageResult> page(PageQueryDTO dto) {
        PageResult pageResult = userService.page(dto);
        return Result.success(pageResult);
    }

    //修改用户
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    //删除用户
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.delete(id);
        return Result.success();
    }
}
